chrome.extension.onMessage.addListener(function(request, sender, sendResponse){
    const {
        username, 
        password, 
        domain, 
        accountId, 
        website
    } = request.message;
    if(location.href.indexOf(domain) === 0){
        $.post('/jz/user/exit.do').complete(()=>{
            const img = document.createElement('img');
            img.src = '/jz/user/allow/imageCode.do?type=2&_=' + new Date().getTime();
            img.style.display = 'none';
            document.body.innerHTML = '<p style="padding:10px;">正在登录...</p>';
            document.body.appendChild(img);
            img.onload = ()=>{
                img.remove()
                const code = recognise(img);
                $.post('/jz/user/allow/login.do', {
                    username,
                    password,
                    code
                }, 'json').done(res => {
                    try{
                        $.ajax({
                            type:'get',
                            url:res.data.ssoDomain + '/u/v1/auth/check_login',
                            data:{
                                resfunc:res.data.resfunc
                            },
                            dataType:'jsonp',
                            jsonp:'callback',
                            jsonpCallback:'user_lbn_jsonpResponse',
                            timeout:30000
                        }).complete(()=>{
                            // location.replace('/clouduser/toInstead.do')
                            if(website == 2 && accountId){
                                location.replace('/platform.html?id=' + accountId)
                            }
                            else{
                                location.replace('/cloud/home.html')
                            }
                        })
                    }
                    catch(e){

                    }
                })
            }
        })
    }
    else{
        sendResponse('wait')
        location.href = domain + 'jz/user/allow/login.do'
    }
})